package com.zsix.system.strategy.userByTaskRule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import com.zsix.common.util.StringUtils;
import com.zsix.system.mapper.taskAssigneeMapper.GenerWorkMapper;
import com.zsix.system.service.IAssigneeUserService;
import com.zsix.common.entity.taskAssignee.AssigneeUserEntity;
import com.zsix.common.entity.taskAssignee.GenerWork;
import com.zsix.common.util.RedisUtils;
import com.zsix.common.util.HttpsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

/**
 * 根据WebAPI设置节点处理人,多人处理模式
 */
@Component
public class ByWebAPIList implements UserByTaskRuleStrategy {
    @Autowired
    private IAssigneeUserService assigneeUserService;
    @Autowired
    private GenerWorkMapper generWorkMapper;
    @Autowired
    private RedisUtils redisUtils;
    public HashMap<String,Object> getUserByTaskRule(String taskid, String ruleName
            , HashMap<String,Object> variables, String procInstId) {
        //获取设置的接收人数据
        HashMap<String, Object> hashMap = assigneeUserService
                .getAssigneeUserWithTaskKey(taskid, ruleName);
        List<AssigneeUserEntity> assigneeUserEntityList = (List<AssigneeUserEntity>) hashMap.get("assigneeUserEntityList");
        //获取当前登陆人信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        String nextWorkEmp = "";
        List<String> byWebAPIList=new ArrayList<>();
        for (AssigneeUserEntity ae : assigneeUserEntityList) {
            GenerWork generWork=generWorkMapper.getGenerWorkByInst(procInstId);
            //增加header参数
            java.util.Map<String, String> headerMap = new Hashtable<String, String>();
            Base64 base64 = new Base64();
            headerMap.put("Content-Type", "application/json");
            headerMap.put("charset", "utf-8");

            //请求参数
            HashMap<String,Object> objectHashMap=new HashMap<>();
            //业务数据主键
            objectHashMap.put("businessKey",generWork.getId());
            //流程运行实例id
            objectHashMap.put("proce_inst_id",generWork.getProce_inst_id());
            //业务表数据
            objectHashMap.put("others",generWork.getData_json());

            try {
                //请求返回信息json
                JSONObject json = null;
                String jsonData = HttpsUtils.post(ae.getUsername(), headerMap, objectHashMap.toString());
                json=JSONObject.parseObject(jsonData);
                JSONArray jsonArray=JSONArray.parseArray(json.get("result").toString());
                for(int i=0;i<jsonArray.size();i++){
                    JSONObject jsonObject=jsonArray.getJSONObject(i);
                    if(nextWorkEmp.contains(jsonObject.get("username").toString()+","))
                        continue;
                    byWebAPIList.add(jsonObject.get("username").toString());
                    nextWorkEmp += jsonObject.get("username").toString() + ",";
                }
            }
            catch (Exception ex){
                nextWorkEmp=username+",";
            }
        }
        if(!StringUtils.isNull(nextWorkEmp)){
            nextWorkEmp = nextWorkEmp.substring(0, nextWorkEmp.length() - 1);
        }
        //放入缓存，用于发送消息推送
        variables.put(taskid + "_byWebAPIList", byWebAPIList);
        if(redisUtils.exists(procInstId+"_sms")){
            redisUtils.remove(procInstId+"_sms");
        }
        redisUtils.set(procInstId+"_sms",nextWorkEmp);
        return variables;
    }
    @Override
    public String getType(){
        return "byWebAPIList";
    }
}
